import {Component, ViewChild} from '@angular/core';
import {Events, IonicPage, NavController, Platform, Tabs} from "ionic-angular";
import 'rxjs';
import {InfoProvider} from "../../providers/InfoProvider";
import {CommProvider} from "../../providers/CommProvider";
import {JPush} from "ionic3-jpush";
import {BackButton} from "../../providers/BackButton";
import {NativeProvider} from "../../providers/NativeProvider";

@IonicPage()
@Component({
  templateUrl: 'tabs.html'
})
export class TabsPage {

    @ViewChild('mainTabs') tabRef: Tabs;
    tabHomeRoot = 'HomePage';
    tabNewsRoot = 'NewsPage';
    tabStrategyRoot = 'StrategyPage';
    tabQuestionRoot = 'QuestionPage';
    tabMineRoot = 'MinePage';

    pushInfo: any = {
        open: '',
        notification: '',
        message: ''
    };

    constructor(public navCtrl: NavController, private platform: Platform,
                private events: Events,
                private jPush: JPush,
                private infoProvider: InfoProvider,
                private commProvider: CommProvider,
                private nativeProvider: NativeProvider,
                private backButton: BackButton) {
        platform.ready().then(ready => {
            if (this.infoProvider.isNative()) {
                this.backButton.registerBackButtonAction(this.tabRef);

                let userInfo = this.infoProvider.getUserInfo();
                this.setupJPush3(userInfo);

                this.checkAppUpdateNeed().then(result => {
                    console.log('checkAppUpdateNeed:', result);
                });
            }
        });
    }

    async checkAppUpdateNeed() {
        if (this.infoProvider.isAndroid()) {
            let updateTip = await this.infoProvider.appUpdateTip();
            if (updateTip) {
                this.nativeProvider.checkAppUpdate().then(data => {
                    console.log('checkAppUpdate:', data);
                });
            }
        }
    }

    setupJPush3(userInfo: any) {
        this.jPush.init().then(res => {
            this.infoProvider.setJPushInit(res);
        }).catch(err => {
            this.pushInfo.error = err;
        });

        this.jPush.getRegistrationID().then(regId => {
            this.infoProvider.setJPushRegId(regId);
            // alert('getRegistrationID then: ' + regId);
        }).catch(err => {
            this.pushInfo.error = err;
            // alert('getRegistrationID catch: ' + err);
        });

        let jPushTags: any[] = ['user'];
        if (userInfo.type == 1) {
            jPushTags.push('vip');
        } else if (userInfo.type > 2) {
            jPushTags.push('admin');
            jPushTags.push('vip');
        }
        this.jPush.setTagsWithAlias(jPushTags, userInfo.phone).then(res => {
            console.log('setTagsWithAlias:', res);
        }).catch(err => {
            this.pushInfo.error = err;
        });

        this.jPush.openNotification()
            .subscribe(res => {
                this.pushInfo.open = res;
                // alert('openNotification: ' + JSON.stringify(res));

                if (this.infoProvider.isAndroid()) {
                  let pushExtra = res.extras['cn.jpush.android.EXTRA'];
                  switch (pushExtra.type) {
                    case 'strategy':
                      this.tabRef.select(2);
                      break;

                    case 'lesson':
                      this.tabRef.select(2);
                      break;

                    case 'qa-create':
                      this.tabRef.select(3);
                      break;

                    case 'qa-reply':
                      this.tabRef.select(3);
                      break;
                  }
                } else if (this.infoProvider.isIos()) {
                  let pushExtra = res.extras;

                  alert('openNotification type: ' + pushExtra.type);
                  switch (pushExtra.type) {
                    case 'strategy':
                      this.tabRef.select(2);
                      break;

                    case 'lesson':
                      this.tabRef.select(2);
                      break;

                    case 'qa-create':
                      this.tabRef.select(3);
                      break;

                    case 'qa-reply':
                      this.tabRef.select(3);
                      break;
                  }
                }
            });

        this.jPush.receiveNotification()
            .subscribe(res => {
                this.pushInfo.notification = res;
                // alert('receiveNotification: ' + JSON.stringify(res));
            });

        this.jPush.receiveMessage()
            .subscribe(res => {
                this.pushInfo.message = res;
                // alert('receiveMessage: ' + JSON.stringify(res));

                if (this.infoProvider.isAndroid()) {
                    this.commProvider.showToast(res.message);
                    let pushType = res.extras['cn.jpush.android.CONTENT_TYPE'];
                    let pushExtra = res.extras['cn.jpush.android.EXTRA'];
                    this.events.publish('push:' + pushType, pushExtra);
                } else if (this.infoProvider.isIos()) {
                    this.commProvider.showToast(res.content);
                    let pushType = res.content_type;
                    let pushExtra = res.extras;
                    this.events.publish('push:' + pushType, pushExtra);
                }
            });
    }
}
